package com.ququ.syb.entrance.service;

import com.ququ.common.result.Result;
import com.ququ.syb.common.constant.EncryptTypeConstant;
import com.ququ.syb.common.constant.ResultStatusConstant;
import com.ququ.syb.common.pojo.TransferOrder;
import com.ququ.syb.entrance.fegin.TransferBusinessFegin;
import com.ququ.syb.entrance.po.RequestData;
import com.ququ.syb.entrance.vo.SybResult;
import com.ququ.syb.entrance.vo.SybResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TransferService {
    @Autowired
    private TransferBusinessFegin transferBusinessFegin;
    @Autowired
    private SecurityService securityService;

    public SybResult transfer(RequestData<String> requestData) {
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1, "加密方式不正确", securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String, Object> dataMap = securityService.getParametersMap(requestData);
        String accountid = requestData.getAccountid();
        String appcode = requestData.getAppcode();
        if (!dataMap.containsKey("amount") || dataMap.get("amount")==null )
            return new SybResult(-1,"缺少转账金额参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Long amount = Long.parseLong(dataMap.get("amount").toString());
        if (!dataMap.containsKey("outorderno") || dataMap.get("outorderno")==null )
            return new SybResult(-1,"缺少外部订单号参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String outorderno = dataMap.get("outorderno").toString();
        if (!dataMap.containsKey("frompaychannelid") || dataMap.get("frompaychannelid")==null )
            return new SybResult(-1,"缺少源通道号参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String frompaychannelid = dataMap.get("frompaychannelid").toString();
        if (!dataMap.containsKey("toaccount") || dataMap.get("toaccount")==null )
            return new SybResult(-1,"缺少目标账户参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String toaccount = dataMap.get("toaccount").toString();
        if (!dataMap.containsKey("topaychannelid") || dataMap.get("topaychannelid")==null )
            return new SybResult(-1,"缺少目标通道号参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String topaychannelid = dataMap.get("topaychannelid").toString();
        Integer type = dataMap.containsKey("type")?Integer.parseInt(dataMap.get("type").toString()):null;

        Result<TransferOrder> transferOrderResult = transferBusinessFegin.transferBusiness(outorderno,accountid,frompaychannelid,toaccount,topaychannelid,amount,type,appcode);
        if (transferOrderResult.getStatus() == ResultStatusConstant.SUCCESS) {
            return securityService.buildSybResult(transferOrderResult.getData(), requestData);
        }else
            return new SybResult(-1,transferOrderResult.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
    }

}
